import config from "@/config/index";
//import webStorageCache  from 'web-storage-cache';
import Vue from 'vue'
// import VueCookies from 'vue-cookies'
// Vue.use(VueCookies)

function _getExpiresDate (expires, now) {
    now = now || new Date();
    if (typeof expires === 'number') {
        //console.log('number')
        expires = new Date(now.getTime() + expires * 1000);
    } else if (typeof expires === 'string') {
        expires = new Date(expires.replace('/-/g','/'));
    }
    return expires.getTime();
}

export default new class Store{
    constructor(){
        this.prefix=config.db_prefix;
        this.store=window.localStorage;
    }
    setItem(key,value,exptime){
        var t=_getExpiresDate(exptime);
        var data={
            value:value,
            exptime:t
        }
        this.store.setItem(this.prefix+key,JSON.stringify(data))
    }
    getItem(key){
        var t=this.store.getItem(this.prefix+key);
        if(t && t.length>0){
            try{
                t=JSON.parse(t);
            }catch{
                t='';
            }
            if(typeof t=='object'){
                var time=t.exptime;
                var n=new Date().getTime();
                if(time>n){
                    return t.value;
                }
            }
            this.removeItem(key)
            return '';
        }
    }
    removeItem(key){
        this.store.removeItem(this.prefix+key);
    }
}